{include file="public/header"}
<body>
	<div class="sectionY">
		<a href="{:url('Paytype/index')}">
			<img src="__PUBLIC__/home/images/a2_1.png" alt="" class="a1">
		</a>
		<p class="p1">绑定支付宝支付</p>
	</div>
	<div class="kong1"></div>
<form class='form_binding' method="post" enctype="multipart/form-data">
	<div class="res_section1">
		<div class="all_1">
			<div class="f1">
				<span class="s1 ">姓名：</span>
				<input type="text" name='username' value="{$userinfo.emailname}" class="input1" placeholder="支付宝用户名/姓名">
			</div>
			<div class="f1">
				<span class="s1 ">支付宝：</span>
				<input type="text" name='alipay' value="{$userinfo.email}" class="input1" placeholder="支付宝账号">
			</div>
			<div class="f1">
				<span class="s1 ">收款码：</span>
				<span class="s2 pic_btn" style="border-radius: 5px; border: 1px solid #FFA500; background-color: #FFA500; color: #fff;padding: 2px;">选择收款码</span>
				<input type="file" id="file" onchange="imgChange(this);" style="width: 0px;height:0px;">
				<div class="dhh">
					<img id="imghead" src="{$userinfo.emailcode}" alt="" style="width: 120px; height: 120px;">
				</div>
			</div>
			<div class="f1">
				<span class="s1 ">交易密码：</span>
				<input type="password" name='pwd' class="input1" placeholder="交易密码">
			</div>
			<div class="sub">
				<input type="hidden" id="upload_geturl">
				<input type="button" value="确认提交" onclick='return check_repwd();' />
			</div>
		</div>
	</div>
</form>
</body>
<script>
    var is_app=0;
    try {
        is_app = android.getType();
    }catch (e) {
    }
    $(function(){
        $(".pic_btn").click(function(){
            if(is_app==1){
                android.uploadImage(location.origin+"/home/Upload/appupimg.html");
            }else{
				$(this).siblings("input").click();
            }
        });
    })
</script>
<script type="text/javascript">
    //安卓回调
    function setImage(img_url) {
		$("#imghead").attr('src',img_url); // 修改img标签src属性值
        $("#upload_geturl").val(img_url);
    }
    function imgChange(obj) {
        var docObj=$(obj).get(0);
        if (docObj.files && docObj.files[0]) {
            if(!/image\/\w+/.test(docObj.files[0].type)){
                //图片文件的type值为image/png或image/jpg
                layer.msg("文件必须为图片！");
                return false;
            }
            var formData = new FormData();
            formData.append("files",docObj.files[0]);
            $.ajax({
                url: "{:url('Upload/upimg')}",
                type: "POST",
                data: formData,
                /**
                 *必须false才会自动加上正确的Content-Type
                 */
                contentType: false,
                /**
                 * 必须false才会避开jQuery对 formdata 的默认处理
                 * XMLHttpRequest会对 formdata 进行正确的处理
                 */
                processData: false,
                success: function (rs) {
                    rs = JSON.parse(rs);
                    if(rs.status==0){
						setImage(rs.data)
                    }else{
                        layer.alert(rs.info,{shade:0.2});return false;
                    }
                },
                error: function () {
                    layer.msg('系统错误，请稍后重试');
                }
            });
        } else {
            layer.msg('请上传图片');
        }
        return true;
    }
    /*开始检测交易密码和各个选项是否填写 */
    function check_repwd(){
        var name= $('input[name=username]').val();
        var user= $('input[name=alipay]').val();
        var repwd  = $('input[name=pwd]').val();
        if(name==''){
            layer.alert('请填写支付宝昵称或姓名！');return false;
        }
        if(user==''){
            layer.alert('请填写支付宝账号！');return false;
        }
        
        if(repwd==''){
            layer.alert('请填写交易密码！');return false;
        }
        /*ajax检测交易密码*/
        $.ajax({
            type: 'POST',
            url: "{:url('Paytype/check_repwd')}",
            dataType: "html",
            data: "code="+repwd,
            success:function(responsestr){
                if(responsestr==1){
                    var img_url=$('#upload_geturl').val();
                    if(img_url==''){
                        layer.alert('请上传支付宝收款码！');return false;
                    }
                    $.ajax({
                        type: 'POST',
                        url: "{:url('Paytype/doalipay')}",
                        dataType: "json",
                        data: {username:name,alipay:user,upload_geturl:img_url},
                        success:function(re){
                            var obj=re;
                            if(obj.s==1){
                                layer.msg(obj.msg,{shade:0.6,time:1000,end:function(){
                                        window.location.href=window.location.href;
                                }});
                            }else{
                                layer.alert(obj.msg,{shade:0.2});return false;
                            }
                        },
                        error:function(){
                            layer.alert('系统繁忙，请稍后再试！',{shade:0.2});
                        }
                    });
                }else{
                    layer.alert(responsestr,{shade:0.2});
                }
            },
            error:function(){
                layer.alert('系统繁忙，请稍后再试！',{shade:0.2});
            }
        });
    }
</script>
</html>
